package light.network.object;

import java.io.Writer;
import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.Map;

import light.network.ByteData;

import org.json.JSONException;
import org.json.JSONObject;

public class PackObject {// extends JSONObject

	//JSONObject param;
	JSONObject header;
	ByteData data;
	
	int headerLength;
	int dataLength;
	
	public PackObject()
	{
		header = new JSONObject();
	}
	public PackObject(Map<String, Object> src)
	{
		header = new JSONObject(src);
	}
	public PackObject(JSONObject src)
	{
		header = src;
	}
	public PackObject(Map<String, Object> src, ByteBuffer buf)
	{
		header = new JSONObject(src);
		data = new ByteData(buf.array(), buf.position(), buf.limit()-buf.position());
	}
	
	public void setHeader(JSONObject header)
	{
		this.header = header;
	}
	
	public JSONObject getHeader()
	{
		return header;
	}
	
	public ByteData array() {
		return data;
	}

	public void setData(ByteData data) {
		this.data = data;
	}


	public int getHeaderLength() {
		return headerLength;
	}
	public void setHeaderLength(int headerLength) {
		this.headerLength = headerLength;
	}
	public int getDataLength() {
		return dataLength;
	}
	public void setDataLength(int dataLength) {
		this.dataLength = dataLength;
	}

	public static void main(String[] args) throws JSONException {
//		TransObject t = new TransObject();
//		t.put("h", "H");
//		
//		JSONObject json = new JSONObject();
//		json.put("t", t);
//		
//		System.out.println(json);
	}
	public Object get(String key) {
		try {
			return header.get(key);
		} catch (JSONException e) {
			return null;
		}
	}
	public boolean getBoolean(String key) {
		try {
			return header.getBoolean(key);
		} catch (JSONException e) {
			throw new RuntimeException(e);
		}
	}
	public double getDouble(String key) {
		try {
			return header.getDouble(key);
		} catch (JSONException e) {
			throw new RuntimeException(e);
		}
	}
	public int getInt(String key) {
		try {
			return header.getInt(key);
		} catch (JSONException e) {
			throw new RuntimeException(e);
		}
	}
	public long getLong(String key) {
		try {
			return header.getLong(key);
		} catch (JSONException e) {
			throw new RuntimeException(e);
		}
	}
	public String getString(String key) {
		try {
			return header.getString(key);
		} catch (JSONException e) {
			return null;
		}
	}
	public boolean has(String key) {
		return header.has(key);
	}
	public Iterator<?> keys() {
		return header.keys();
	}
	public int length() {
		return header.length();
	}
	public PackObject put(String key, Object value) {
		try {
			header.put(key, value);
			return this;
		} catch (JSONException e) {
			throw new RuntimeException(e);
		}
	}
	public Object remove(String key) {
		return header.remove(key);
	}
	public Writer write(Writer writer) {
		try {
			return header.write(writer);
		} catch (JSONException e) {
			throw new RuntimeException(e);
		}
	}
	
	
}
